package com.jhhc.StormSurgeForecast.AstronomicalTide.ServiceImpl;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.BDMS_FBC_TFPATH_FMapper;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_F;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.ForecastPath;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.ForecastPaths;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFPATH_FService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (BdmsFbcTfpathF)表服务实现类
 *
 * @author makejava
 * @since 2023-06-25 11:34:18
 */
@Service("BDMS_FBC_TFPATH_FService")
public class BDMS_FBC_TFPATH_FServiceImpl implements BDMS_FBC_TFPATH_FService {

    @Resource
    private BDMS_FBC_TFPATH_FMapper fMapper;

    @Override
    public List<BDMS_FBC_TFPATH_F> selectBytfid(String tfid, String dept, String startTime, String endTime) {
        return fMapper.selectBytfid(tfid, dept, startTime, endTime);
    }

    @Override
    public List<ForecastPath> getForecastPaths(String typhoonId, String realTime) {
        List<BDMS_FBC_TFPATH_F> fList = fMapper.getForecastPaths(typhoonId, realTime);
        if (fList != null && fList.size() > 0) {
            List<ForecastPath> forecastPaths = new ArrayList<>();
            ForecastPath forecastPath = null;
            //BDMS_FBC_TFPATH_F.getForecastdept去重
            List<String> forecastdept = fList.stream().map(BDMS_FBC_TFPATH_F::getForecastdept).distinct()
                    .collect(Collectors.toList());
            List<BDMS_FBC_TFPATH_F> dept = null;
            for (String s : forecastdept) {
                forecastPath = new ForecastPath();
                forecastPath.setDepartment(s);
                dept = fList.stream().filter(x -> Objects.equals(x.getForecastdept(), s)).collect(Collectors.toList());
                List<ForecastPaths> forecastPathsList = new ArrayList<>();
                ForecastPaths f = null;
                for (BDMS_FBC_TFPATH_F bdmsFbcTfpathF : dept) {
                    f = new ForecastPaths();
                    if (bdmsFbcTfpathF.getForecasttm() != null && !"".equals(bdmsFbcTfpathF.getForecasttm())) {
                        f.setForecastTime(bdmsFbcTfpathF.getForecasttm());
                    }
                    if (bdmsFbcTfpathF.getLttd() != null) {
                        f.setLttd(bdmsFbcTfpathF.getLttd());
                    }
                    if (bdmsFbcTfpathF.getLgtd() != null) {
                        f.setLgtd(bdmsFbcTfpathF.getLgtd());
                    }
                    if (bdmsFbcTfpathF.getPressure() != null) {
                        f.setPressure(bdmsFbcTfpathF.getPressure());
                    }
                    if (bdmsFbcTfpathF.getMaxspeed() != null) {
                        f.setMaxSpeed(bdmsFbcTfpathF.getMaxspeed());
                    }
                    forecastPathsList.add(f);
                }
                forecastPath.setForecastPaths(forecastPathsList);
                forecastPaths.add(forecastPath);
                f = null;
            }
            forecastPath = null;
            return forecastPaths;
        }else {
            return null;
        }
    }

    @Override
    public List<BDMS_FBC_TFPATH_F> forecastPath(String tfid, String typhoonDepart, String forecastTime) {
        return fMapper.forecastPath(tfid, typhoonDepart, forecastTime);
    }

    @Override
    public List<BDMS_FBC_TFPATH_F> queryByTfidAll(String typhoonId) {
        List<BDMS_FBC_TFPATH_F> fList = fMapper.queryByTfidAll(typhoonId);
        if (fList != null && fList.size() > 0) {
            return fList;
        }else {
            return null;
        }
    }

    @Override
    public List<ForecastPath> getTyphoonAll(List<BDMS_FBC_TFPATH_F> fList) {
        if (fList != null && fList.size() > 0) {
            List<ForecastPath> forecastPaths = new ArrayList<>();
            ForecastPath forecastPath = null;
            //BDMS_FBC_TFPATH_F.getForecastdept去重
            List<String> forecastdept = fList.stream().map(BDMS_FBC_TFPATH_F::getForecastdept).distinct()
                    .collect(Collectors.toList());
            List<BDMS_FBC_TFPATH_F> dept = null;
            for (String s : forecastdept) {
                forecastPath = new ForecastPath();
                forecastPath.setDepartment(s);
                dept = fList.stream().filter(x -> Objects.equals(x.getForecastdept(), s)).collect(Collectors.toList());
                List<ForecastPaths> forecastPathsList = new ArrayList<>();
                ForecastPaths f = null;
                for (BDMS_FBC_TFPATH_F bdmsFbcTfpathF : dept) {
                    f = new ForecastPaths();
                    if (bdmsFbcTfpathF.getForecasttm() != null && !"".equals(bdmsFbcTfpathF.getForecasttm())) {
                        f.setForecastTime(bdmsFbcTfpathF.getForecasttm());
                    }
                    if (bdmsFbcTfpathF.getLttd() != null) {
                        f.setLttd(bdmsFbcTfpathF.getLttd());
                    }
                    if (bdmsFbcTfpathF.getLgtd() != null) {
                        f.setLgtd(bdmsFbcTfpathF.getLgtd());
                    }
                    if (bdmsFbcTfpathF.getPressure() != null) {
                        f.setPressure(bdmsFbcTfpathF.getPressure());
                    }
                    if (bdmsFbcTfpathF.getMaxspeed() != null) {
                        f.setMaxSpeed(bdmsFbcTfpathF.getMaxspeed());
                    }
                    forecastPathsList.add(f);
                }
                forecastPath.setForecastPaths(forecastPathsList);
                forecastPaths.add(forecastPath);
                f = null;
            }
            forecastPath = null;
            return forecastPaths;
        }else {
            return null;
        }
    }
}
